package com.mobimap.nhom9.gui;

import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.mobimap.nhom9.bus.IPlaceBUS;
import com.mobimap.nhom9.bus.MobimapProvider;
import com.mobimap.nhom9.dto.LocationDTO;
import com.mobimap.nhom9.dto.PlaceExDTO;
import com.mobimap.nhom9.gui.utls.LocateUtils;

public class MainActivity extends AbstractActivity implements OnClickListener {
	Button bt_zoomin;
	Button bt_zoomout;
	Container container;
	MapView mapview;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MobimapProvider.createInstance(this);
		Container.InitContainer(this);
		setContentView(R.layout.activity_main);

		container = new Container(this);
		bt_zoomin = (Button) findViewById(R.id.bt_zoomin);
		bt_zoomout = (Button) findViewById(R.id.bt_zoomout);

		bt_zoomin.setOnClickListener(this);
		bt_zoomout.setOnClickListener(this);
		mapview = (MapView) findViewById(R.id.MapView1);
		// mapview.setCenterLocation(new LocationDTO(-72.35558118969126,
		// 106.74247741699219));
		Point p = mapview.getPosition();
		LocationDTO l = LocateUtils.ToGeographicCoordinate(p, Container
				.getInstance().getZoom());
		Point p2 = LocateUtils.ToPoint(l, container.getZoom());
		mapview.setPosition(p2);
		
		mapview.setDirections(new LocationDTO(10.762795, 106.682568), new LocationDTO(10.764988, 106.681731));
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (this.isMark)	{
			IPlaceBUS placeBus = MobimapProvider.getPlaceBus(this);
			PlaceExDTO p = placeBus.getPlaceExDto(this.id, this.place_type);
			mapview.setMarker(p);
			mapview.setCenterLocation(p.getLocation());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onProcess(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected int getLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_main;
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		LocationDTO l = mapview.getCenterLocation();
		switch (v.getId()) {
		case R.id.bt_zoomin: {
			int maxZoom = Container.getInstance().getMaxZoom();
			Container.getInstance().setZoom(maxZoom);
			Log.i("Zoomin", String.valueOf(maxZoom));
			Toast.makeText(this, "Zoomin " + String.valueOf(maxZoom),
					Toast.LENGTH_SHORT).show();
			break;
		}
		case R.id.bt_zoomout: {
			int minZoom = Container.getInstance().getMinZoom();
			Container.getInstance().setZoom(minZoom);
			Log.i("Zoomin", String.valueOf(minZoom));
			Toast.makeText(this, "Zoomout " + String.valueOf(minZoom),
					Toast.LENGTH_SHORT).show();
			break;
		}
		}
		mapview.setCenterLocation(l);
	}

}
